package com.yupi.apiback.model.enums.user;

import cn.hutool.core.util.StrUtil;
import com.yupi.apiback.common.ErrorCode;
import com.yupi.apiback.exception.BusinessException;

/**
 * 用户角色
 *
 * @author yanghaifeng
 * @date 2024/1/18 16:21
 */
public enum UserRoleEnum {
    ADMIN("admin", "管理员"),
    USER("user", "普通用户");

    UserRoleEnum(String key, String remark) {
        this.key = key;
        this.remark = remark;
    }

    private final String key;
    private final String remark;

    public String getKey() {
        return key;
    }

    public String getRemark() {
        return remark;
    }

    public static UserRoleEnum getKeyEnum(String key) {
        if (StrUtil.isBlank(key)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户角色枚举不匹配");
        }
        UserRoleEnum[] values = values();
        for (UserRoleEnum value : values) {
            if (value.key.equals(key)) {
                return value;
            }
        }
        throw new BusinessException(ErrorCode.OPERATION_ERROR, "用户角色枚举不匹配");
    }
}
